package edu.neu.swc.skynet.entity;

import com.alibaba.fastjson.JSON;
import edu.neu.swc.skynet.document.MovieDoc;
import edu.neu.swc.skynet.movie.MovieInfo;
import edu.neu.swc.skynet.movie.MovieShow;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.text.SimpleDateFormat;
import java.util.Date;

@NoArgsConstructor
@AllArgsConstructor
@Data
@Entity
@EntityListeners(AuditingEntityListener.class)
@Table(name = "t_skynet_movie")
public class SkyNetMovie {

    /** 自增主键 */
    @Id
    @Column(name = "id", nullable = false)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    /** 电影名称 */
    @Column(name = "movie_name", nullable = false)
    private String movieName;

    /** 电影图片 */
    @Column(name = "movie_image", nullable = false)
    private String movieImage;

    /** 电影导演 */
    @Column(name = "movie_director", nullable = false)
    private String movieDirector;

    /** 电影主演 */
    @Column(name = "movie_main", nullable = false)
    private String movieMain;

    /** 电影演员表 */
    @Column(name = "movie_actor", nullable = false)
    private String movieActor;

    /** 电影上映日期 */
    @Column(name = "movie_release", nullable = false)
    private Date movieRelease;

    /** 电影出品国家 */
    @Column(name = "movie_location", nullable = false)
    private String movieLocation;

    /** 电影其他信息，作冗余字段集合 */
    @Column(name = "movie_properties", nullable = false)
    private String movieProperties;

    /** 创建时间 */
    @CreatedDate
    @Column(name = "create_time", nullable = false)
    private Date createTime;

    /** 更新时间 */
    @LastModifiedDate
    @Column(name = "update_time", nullable = false)
    private Date updateTime;

    /**
     * <h2>MovieInfo -> SkyNetMovie</h2>
     * @param movieInfo 待转换的对象
     * @return 转换完成的SkynetMovie对象
     */
    public static SkyNetMovie to(MovieInfo movieInfo){

        SkyNetMovie skyNetMovie = new SkyNetMovie();

        skyNetMovie.setId(movieInfo.getId());
        skyNetMovie.setMovieName(movieInfo.getMovieName());
        skyNetMovie.setMovieImage(movieInfo.getMovieImage());
        skyNetMovie.setMovieDirector(movieInfo.getMovieDirector());
        skyNetMovie.setMovieMain(movieInfo.getMovieMain());
        skyNetMovie.setMovieLocation(movieInfo.getMovieLocation());
        skyNetMovie.setMovieRelease(movieInfo.getMovieRelease());
        skyNetMovie.setMovieActor(
                JSON.toJSONString(movieInfo.getMovieActor())
        );
        skyNetMovie.setMovieProperties(
                JSON.toJSONString(movieInfo.getMovieProperties())
        );

        return skyNetMovie;
    }

    /**
     * <h2>SkyNetMovie -> MovieInfo</h2>
     * @return 转换后的 MovieInfo 对象
     */
    public MovieInfo toMovieInfo(){

        MovieInfo movieInfo = new MovieInfo();

        movieInfo.setId(this.getId());
        movieInfo.setMovieName(this.getMovieName());
        movieInfo.setMovieImage(this.getMovieImage());
        movieInfo.setMovieDirector(this.getMovieDirector());
        movieInfo.setMovieMain(this.getMovieMain());
        movieInfo.setMovieRelease(this.getMovieRelease());
        movieInfo.setMovieLocation(this.getMovieLocation());
        movieInfo.setMovieActor(JSON.parseObject(
                this.getMovieActor(),
                MovieInfo.MovieActor.class
        ));
        movieInfo.setMovieProperties(JSON.parseObject(
                this.getMovieProperties(),
                MovieInfo.MovieProperties.class
        ));

        return movieInfo;
    }

    /**
     * <h2>SkyNetMovie -> MovieShow</h2>
     * @return 转化完成的 MovieShow 对象
     */
    public MovieShow toMovieShow(){

        MovieShow movieShow = new MovieShow();

        movieShow.setId(this.getId());
        movieShow.setMovieName(this.getMovieName());
        movieShow.setMovieImage(this.getMovieImage());
        movieShow.setMovieDirector(this.getMovieDirector());
        movieShow.setMovieMain(this.getMovieMain());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy");
        movieShow.setMovieRelease(simpleDateFormat.format(this.getMovieRelease()));
        movieShow.setMovieLocation(this.getMovieLocation());

        return movieShow;
    }

    /**
     * <h2>SkyNetMovie -> MovieDoc</h2>
     * @return 转化完成的 MovieDoc
     */
    public MovieDoc toMovieDoc(){
        MovieDoc movieDoc = new MovieDoc();

        movieDoc.setId(this.getId());
        movieDoc.setMovieName(this.getMovieName());
        movieDoc.setMovieImage(this.getMovieImage());
        movieDoc.setMovieDirector(this.getMovieDirector());
        movieDoc.setMovieMain(this.getMovieMain());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy");
        movieDoc.setMovieRelease(simpleDateFormat.format(this.getMovieRelease()));
        movieDoc.setMovieLocation(this.getMovieLocation());

        return movieDoc;
    }
}
